package com.example.SocketSelectorStudy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

/**
 * Created by Administrator on 2017/5/24.
 */
public class SelectorClient {


    public static void main(String args[]) {
        work();
    }

    public static void work() {
        int serverPort = 8888;
        ByteBuffer sendBuffer = ByteBuffer.wrap("client message".getBytes());
        ByteBuffer receiveBuffer = ByteBuffer.allocate(1024);
        try {
            //创建通道，设置通道注册到selector中
            SocketChannel socketChannel = SocketChannel.open();
            Selector selector = Selector.open();

            socketChannel.configureBlocking(false);
            socketChannel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ | SelectionKey.OP_CONNECT);
            socketChannel.connect(new InetSocketAddress("localhost", serverPort));
            int executeTimes = 20;
            //while
            while (executeTimes > 0) {
                executeTimes--;
                int readyChannelNum = selector.select();
                if (readyChannelNum == 0) {
                    continue;
                }
                Set<SelectionKey> setOfSelectionKey = selector.selectedKeys();
                Iterator<SelectionKey> iterator = setOfSelectionKey.iterator();
                while (iterator.hasNext()) {
                    SelectionKey selectionKey = iterator.next();
                    SocketChannel socketChannel1 = (SocketChannel) selectionKey.channel();
                    dealSelectionKey(sendBuffer, receiveBuffer, selectionKey, socketChannel1);
                    iterator.remove();
                }
            }
            socketChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void dealSelectionKey(ByteBuffer sendBuffer, ByteBuffer receiveBuffer, SelectionKey selectionKey, SocketChannel socketChannel1) throws IOException {
        if (selectionKey.isConnectable()) {
            if (socketChannel1.isConnectionPending()) {
                socketChannel1.finishConnect();
                System.out.println("connection complete.");

            }
        }//if isConnectable
        else if (selectionKey.isReadable()) {
            receiveBuffer.clear();
            socketChannel1.read(receiveBuffer);
            receiveBuffer.flip();
            System.out.println("message from server: " + new String(receiveBuffer.array()));

        }//else if readable
        else if (selectionKey.isWritable()) {
            sendBuffer.flip();
            Scanner s = new Scanner(System.in);
            String str = s.next();
            sendBuffer = ByteBuffer.wrap(str.getBytes());
            socketChannel1.write(sendBuffer);
            socketChannel1.write(sendBuffer);
        }
    }

}
